package
 {
	import org.cove.ape.*;

	import flash.events.*;
	import flash.display.*;
	import flash.utils.*;

	import caurina.transitions.Tweener;

	public class View
	{
		//variabile de stare pt panLogic
		//last panning key
		public var lastKey:String;
		public var currentPanningDir:int;
		private var targetPos:int = 0;
		
		//variabile pt prima metoda de panning aia primitiva
//		public var forcePanX:int = 0;
//		public var panDistance:int = 0;
//		private var isPanning:Boolean;


		
		
		private var model:IModel;
		private var controller:IKeyboardInputHandler;
		private var paper:Sprite;
		
		
		public function View(aModel:IModel, oController:IKeyboardInputHandler,target:Stage,oContainer:Sprite)
		{			
			this.model = aModel;
			this.controller = oController;
			this.paper=oContainer;
			
			lastKey="none";
			targetPos=300-(model as Model).car.px;
			
			// register to receive notifications from the model
			model.addEventListener(Event.CHANGE, this.update);	
			
			// register to receive key press notifications from the stage
			// but delegate to the controller	
			target.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
			
			// register to receive notifications from the controller
			// thing is right now the model constantly keeps bothering the view
			// in a future model this might not happen - thus the controller will need to report key CHANGES by himself
			controller.addEventListener(Event.CHANGE, this.update);

			
		}
		
		//as i said before panLogic is dependent on CHANGES of "targetedcar.x" and "lastKey"
		//it does some logc and then uses Tweener to get the transitions done
		public function panLogic():void
		{
			//trace("dir:",currentPanningDir)
			if  (APEngine.container.x==targetPos) currentPanningDir=0;
			if (currentPanningDir==-1)
			{
				if (lastKey=="right") 
				{
					currentPanningDir=0;
					lastKey="none";
				} 
				
				if (lastKey=="left") 
				{
					targetPos+=50;
					Tweener.addTween(APEngine.container,{
												x:targetPos,
												time:1,
												transition:"easeout"
														})
					lastKey="none";
				}
			}
			
			if (currentPanningDir==1)
			{
				if (lastKey=="left")
				{
					currentPanningDir=0;
					lastKey="none";
				}
				
				if (lastKey=="right") 
				{
					targetPos-=50;
					Tweener.addTween(APEngine.container,{
												x:targetPos,
												time:1,
												transition:"easeout"
														})
					lastKey="none";
				}
				
			}
			
			//camera nu este in miscare nici stanga nici dreapta
			//adica currentPanningDir==0 oprit, -1 stanga, 1 dreapta 
			if (currentPanningDir==0) 
			{
				//daca nu s-a paasat nici o tasta pt Panning camera urmareste masina
				//cu o libertate de miscare fata de masina de 40 -(-20)+20
				//miscarea de urmarire nu se considera Panning !
				if (lastKey=="none")
				{
					//trace("Enter folow follow;APEngine.container.x ",APEngine.container.x,"((model as Model).car.px+20)",((model as Model).car.px+20));
					if ( APEngine.container.x > (300-(model as Model).car.px+20) )
					{
						Tweener.addTween(APEngine.container,{
													x:(300-(model as Model).car.px+20),
													time:1,
													transition:"easeout"
															})
						//trace("folow follow left! ");									
					}
															
					else if ( APEngine.container.x < (300-(model as Model).car.px-20) )
					{
						Tweener.addTween(APEngine.container,{
													x:(300-(model as Model).car.px-20),
													time:1,
													transition:"easeout"
															})
						//trace("folow follow right! ");									
					}
															
				}
				//daca se apasa "dreapta"
				if (lastKey=="right")
				{					
					lastKey="none";
					currentPanningDir=1;
					targetPos=300-(model as Model).car.px-150;
					Tweener.addTween(APEngine.container,{
													x:targetPos,
													time:2,
													transition:"easeout"
														})
				}
				//daca se apasa "stanga"
				if (lastKey=="left")
				{					
					lastKey="none";
					currentPanningDir=-1;
					targetPos=300-(model as Model).car.px+150;
					Tweener.addTween(APEngine.container,{
													x:targetPos,
													time:2,
													transition:"easeout"
														})
				}
			}
		}

		private function update(event:Event):void
		{
			// get data from model and update view
			//trace(model.getCarPosition());		
			panLogic();
		}

		private function onKeyPress(event:KeyboardEvent):void
		{
			// delegate to the controller (strategy) to handle it
			controller.keyPressHandler(event);
			
		}
	}
}	